package com.zoro.barn.inner.service.pp.product.controller;

import com.zoro.barn.api.pp.product.BarnProductService;
import com.zoro.barn.commons.tools.json.JsonUtil;
import com.zoro.barn.dto.pp.product.BarnProductInfoDto;
import com.zoro.barn.dto.pp.product.productinfodto.GetProductInfoInfosReq;
import com.zoro.barn.inner.service.pp.product.entity.ProductEntity;
import com.zoro.barn.inner.service.pp.product.service.ProductService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhaoxingwu
 */
@RestController
@RequestMapping("product-info")
public class ProductController implements BarnProductService {

    private final ProductService productService;

    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    @Override
    public Map<String, BarnProductInfoDto> getProductInfos(GetProductInfoInfosReq req) throws Exception {

        List<ProductEntity> list = this.productService.getByIds(Arrays.asList(req.getIds()));
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        List<BarnProductInfoDto> dtoList = JsonUtil.transformType(list, BarnProductInfoDto.class);

        return dtoList.stream().collect(Collectors.toMap(BarnProductInfoDto::getPid, v -> v, (a, b) -> b));
    }
}
